package com.etoak.common.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.xml.crypto.Data;
import java.security.Key;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

public class JwtUtil {
    private static final String SECRET = "1111111111111111111111111111111111111111";

    // 签名的key
    private static final Key KEY = Keys.hmacShaKeyFor(SECRET.getBytes());

    // 有效期:12小时
    private static final long EXPIRE = 1000 * 60 * 60 * 12;

    // 创建jwt
    public static String create(Map<String, Object> claimsMap){
        Date now = new Date();
        Date expiration = new Date(now.getTime()+EXPIRE);
        String compact = Jwts.builder()
                .signWith(KEY)
                .setClaims(claimsMap)
                .setIssuedAt(now)
                .setExpiration(expiration)
                .compact();
        return compact;
    }

    public static Map<String ,Object> parse(String jwt) {
        return Jwts.parserBuilder().setSigningKey(KEY).build().parseClaimsJws(jwt).getBody();
    }
}
